package tasks;

import jade.core.Agent;

import jade.domain.FIPANames;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Random;


public class ContractorAgent extends Agent {
    private Hashtable<Integer, Integer> db;
    
    public static final int MAX_NUM_OF_CONF = 20;
    
    private Random rand = new Random();
    
    private final MessageTemplate templateContractNet = 
          MessageTemplate.and(MessageTemplate.MatchProtocol(
              FIPANames.InteractionProtocol.FIPA_CONTRACT_NET),
              MessageTemplate.MatchPerformative(ACLMessage.CFP));
      
    public ContractorAgent() {
        super();
    }
    
    protected void setup() {
        //generate some random configurations
        //int numOfCfg = 1 + (rand.nextInt(MAX_NUM_OF_CONF));
        int numOfCfg = MAX_NUM_OF_CONF;
        db = ConfigurationManager.generateConfigurations(numOfCfg);
        //add the behaviour for contract net
        addBehaviour(new ContractNetRB(this, templateContractNet));
    }
    
    ArrayList<ArrayList<Long>> generateProposal(ArrayList<Long> task) {
        return ConfigurationManager.matchTask(task, db);
    }
    
    void printOffers(String[] offers) {
        System.out.println("Agent " + getLocalName() + " will supply the " +
                           "following configurations:");
        for(String offer : offers) {
            if(!offer.equals("")) {
                System.out.println(ConfigurationManager.offerToString(
                                     Long.parseLong(offer)));
            }
        }
    }
}
